/**
 * 
 */
package com.ydy.service.account.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ydy.constant.SystemConstant;
import com.ydy.exception.BusinessException;
import com.ydy.exception.DataNotFoundException;
import com.ydy.exception.ValidateException;
import com.ydy.ienum.EnumAccount;
import com.ydy.mapper.AccountMapper;
import com.ydy.model.Account;
import com.ydy.model.Staff;
import com.ydy.service.account.AccountService;
import com.ydy.utils.ValidateUtil;
import com.ydy.vo.other.BaseVo;
import com.ydy.vo.other.PageVo;
import com.ydy.vo.other.ResultVo;

/**
 * @author xuzhaojie
 *
 *         2019年5月29日 上午8:35:26
 */
@Service
@Transactional
public class AccountServiceImpl implements AccountService {
	private final static Logger log = LoggerFactory.getLogger(AccountServiceImpl.class);
	@Autowired
	private AccountMapper accountMapper;

	@Override
	public PageVo<Account> select(Account account, Integer page, Integer size) {
		PageVo<Account> vo = new PageVo<Account>(page, size);
		Page<Account> pageBean = PageHelper.startPage(vo.getPage(), vo.getSize(), "is_default desc,id desc");
		List<Account> list = accountMapper.select(account);
		vo.setTotal(pageBean.getTotal());
		vo.setList(list);
		return vo;
	}

	@Override
	public Account saveOrUpdate(Account account, Staff staff) {
		if (account == null) {
			throw new NullPointerException();
		}
		// 校验数据有效
		Map<String, String> validateInfo = ValidateUtil.validateEntity(account);
		if (!validateInfo.isEmpty()) {
			throw new ValidateException(validateInfo);
		}
		if (staff != null) {
			account.setShopId(staff.getShopId());
		}
		// 新增信息
		if (account.getId() == null) {
			account.setIsDefault(SystemConstant.DEFAULT_YES);
			account.setCreateTime(new Date());
			accountMapper.insertSelective(account);
			log.info("新增account成功：" + account.getId());
		} else {// 根据id更新信息
			Account temp = accountMapper.selectByPrimaryKey(account.getId());
			if (temp == null) {
				log.info("找不到account信息：" + account.getId());
				throw new DataNotFoundException(EnumAccount.DATA_NOT_FOUND);
			}
			accountMapper.updateByPrimaryKeySelective(account);
			account.setCreateTime(temp.getCreateTime());
			account.setIsDefault(temp.getIsDefault());
			log.info("更新account成功：" + account.getId());
		}
		return account;
	}

	@Override
	public BaseVo delete(Long id, Staff staff) {
		if (id == null) {
			throw new NullPointerException();
		}
		Account temp = accountMapper.selectByPrimaryKey(id);
		if (temp == null) {
			log.info("找不到account信息：" + id);
			throw new DataNotFoundException(EnumAccount.DATA_NOT_FOUND);
		}
		if (staff != null) {
			if (!Objects.equals(temp.getShopId(), staff.getShopId())) {
				throw new BusinessException(EnumAccount.NOT_BELONG_SHOP);
			}
		}
		accountMapper.deleteByPrimaryKey(id);
		log.info("删除account成功：" + id);
		return new ResultVo();
	}

	@Override
	public BaseVo setDefault(Long id, Staff staff) {
		if (id == null) {
			throw new NullPointerException();
		}
		Account temp = accountMapper.selectByPrimaryKey(id);
		if (temp == null) {
			log.info("找不到account信息：" + id);
			throw new DataNotFoundException(EnumAccount.DATA_NOT_FOUND);
		}
		if (staff != null) {
			if (!Objects.equals(temp.getShopId(), staff.getShopId())) {
				throw new BusinessException(EnumAccount.NOT_BELONG_SHOP);
			}
		}
		Account account = new Account();
		account.setId(id);
		StringBuilder builder = new StringBuilder();
		if (Objects.equals(SystemConstant.DEFAULT_YES, temp.getIsDefault())) {
			account.setIsDefault(SystemConstant.DEFAULT_NO);
			builder.append("Account非默认成功:").append(id);
		} else {
			account.setIsDefault(SystemConstant.DEFAULT_YES);
			builder.append("Account默认上架成功:").append(id);
		}
		accountMapper.updateByPrimaryKeySelective(account);
		log.info(builder.toString());
		return new ResultVo();
	}

	@Override
	public Account selectById(Long id, Staff staff) {
		if (id == null) {
			throw new NullPointerException();
		}
		Account temp = accountMapper.selectByPrimaryKey(id);
		if (temp == null) {
			throw new BusinessException(EnumAccount.DATA_NOT_FOUND);
		}
		if (staff != null) {
			if (!Objects.equals(temp.getShopId(), staff.getShopId())) {
				throw new BusinessException(EnumAccount.NOT_BELONG_SHOP);
			}
		}
		return temp;
	}

}
